package com.atwulidun.chainofresponsibiliity22;

public abstract class Approver {
    // 当前处理者名称
    protected String name;
    // 下一位处理者
    protected Approver successor;

    public Approver(String name) {
        this.name = name;
    }

    // 通过setter方法来设置下一位处理者
    public void setSuccessor(Approver successor) {
        this.successor = successor;
    }

    // 处理购买请求的抽象方法
    protected abstract void processRequest(PurchaseRequest purchaseRequest);
}

// Approver的具体子类
// 系主任类
class DepartmentApprover extends Approver {
    public DepartmentApprover(String name) {
        super(name);
    }

    @Override
    protected void processRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() <= 5000) {
            System.out.println(purchaseRequest.getDes() + "由" + name + "处理");
        } else {
            // 交给一个处理者来处理
            successor.processRequest(purchaseRequest);
        }
    }
}
// 院长类
class CollegeApprover extends Approver {
    public CollegeApprover(String name) {
        super(name);
    }

    @Override
    protected void processRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() > 5000 && purchaseRequest.getPrice() <= 10000) {
            System.out.println(purchaseRequest.getDes() + "由" + name + "处理");
        } else {
            // 交给一个处理者来处理
            successor.processRequest(purchaseRequest);
        }
    }
}
// 副校长类
class VicePrincipalApprover extends Approver {
    public VicePrincipalApprover(String name) {
        super(name);
    }

    @Override
    protected void processRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() > 10000 && purchaseRequest.getPrice() <= 30000) {
            System.out.println(purchaseRequest.getDes() + "由" + name + "处理");
        } else {
            // 交给一个处理者来处理
            successor.processRequest(purchaseRequest);
        }
    }
}
// 校长类
class PrincipalApprover extends Approver {
    public PrincipalApprover(String name) {
        super(name);
    }

    @Override
    protected void processRequest(PurchaseRequest purchaseRequest) {
        if (purchaseRequest.getPrice() > 30000) {
            System.out.println(purchaseRequest.getDes() + "由" + name + "处理");
        } else {
            // 交给一个处理者来处理
            successor.processRequest(purchaseRequest);
        }
    }
}